package com.open.exam.businessservice;

import com.open.exam.databasemodel.ExamineeAnswerDetail;
import com.open.exam.databasemodel.ExamingMonitor;
import com.open.exam.databasemodel.ExamingMonitorLog;
import com.open.exam.utils.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 考试结果处理
 * @author 奶茶
 * Created by on 2019/3/18 9:53.
 */
@Service
public class ExamResultBusinessService {

    private static final Logger logger = LoggerFactory.getLogger(ExamResultBusinessService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 批量同步考生监控数据
     * @param monitors
     */
    public void synchronizationExamMonitorData(List<ExamingMonitor> monitors){
        String updateSQL = "UPDATE examingmonitor SET ExamStartTime = ?,ExamEndTime = ?,SubmitType = ?,IfDeregulation = ?,RetainTime = ? WHERE AppID = ? AND ExamPlanInfoID = ? AND ExamSubjectID = ? AND ExamineeID = ?";
        jdbcTemplate.batchUpdate(updateSQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ExamingMonitor monitor = monitors.get(i);
                ps.setString(1, monitor.getExamStartTime());
                ps.setString(2, monitor.getExamEndTime());
                ps.setString(3,monitor.getSubmitType().toString());
                ps.setString(4,monitor.getIfDeregulation().toString());
                ps.setString(5,monitor.getRetainTime().toString());
                ps.setString(6,monitor.getAppID());
                ps.setString(7,monitor.getExamPlanInfoID());
                ps.setString(8,monitor.getExamSubjectID());
                ps.setString(9,monitor.getExamineeID());
            }

            @Override
            public int getBatchSize() {
                return monitors.size();
            }
        });
    }

    /**
     * 批量同步考生监控日志数据
     * @param monitorLogs
     */
    public void synchronizationExamMointorLogData(List<ExamingMonitorLog> monitorLogs){
        String insertSQL = "INSERT INTO examingmonitorlog (ExamineeArrangeID,TypeID,LogTime,Memo,Reason) VALUES (?,?,?,?,?)";
        jdbcTemplate.batchUpdate(insertSQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ExamingMonitorLog monitorLog = monitorLogs.get(i);
                ps.setString(1,monitorLog.getExamineeArrangeID());
                ps.setInt(2,monitorLog.getTypeID());
                ps.setString(3,monitorLog.getLogTime());
                ps.setString(4,monitorLog.getMemo());
                ps.setString(5,monitorLog.getReason());
            }

            @Override
            public int getBatchSize() {
                return monitorLogs.size();
            }
        });
    }

    /**
     * 批量同步考生作答数据
     * @param answerDetails
     */
    public void  synchronizationExaminerAnswerDetails(List<ExamineeAnswerDetail> answerDetails){
        String insertORUpdateSQL = "INSERT INTO examineeanswerdetail (ExamineeId,ExamPaperID,ExamPaperItemId,SequenceIndex,IsCompletion,AnswerResults,Score,IsMarked,CreateTime) VALUES (?,?,?,?,?,?,?,?,NOW())\n" +
                "ON DUPLICATE KEY \n" +
                "UPDATE AnswerResults = ?, IsCompletion = ?, IsMarked = ?, CreateTime=NOW()";
        jdbcTemplate.batchUpdate(insertORUpdateSQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ExamineeAnswerDetail examineeAnswerDetail = answerDetails.get(i);
                ps.setString(1,examineeAnswerDetail.getExamineeId());
                ps.setString(2,examineeAnswerDetail.getExamPaperID());
                ps.setString(3,examineeAnswerDetail.getExamPaperItemId());
                ps.setInt(4,examineeAnswerDetail.getSequenceIndex());
                ps.setInt(5,examineeAnswerDetail.getIsCompletion());
                ps.setString(6,examineeAnswerDetail.getAnswerResults());
                ps.setString(7,examineeAnswerDetail.getScore().toString());
                ps.setBoolean(8,examineeAnswerDetail.getMarked());
                ps.setString(9,examineeAnswerDetail.getAnswerResults());
                ps.setInt(10,examineeAnswerDetail.getIsCompletion());
                ps.setBoolean(11,examineeAnswerDetail.getMarked());
            }

            @Override
            public int getBatchSize() {
                return answerDetails.size();
            }
        });
    }

    public static void main(String[] args) {
        List<ExamingMonitorLog> examingMonitorLogs = new ArrayList<>();
        List<ExamineeAnswerDetail> examineeAnswerDetailList = new ArrayList<>();
        for (int i = 0;i<4;i++){
            ExamineeAnswerDetail examineeAnswerDetail = new ExamineeAnswerDetail();
            examineeAnswerDetail.setExamineeId("111");
            examineeAnswerDetail.setExamPaperID("222");
            examineeAnswerDetail.setExamPaperItemId("222-1");
            examineeAnswerDetail.setAnswerResults(String.valueOf(i));
            examineeAnswerDetail.setIsCompletion(1);
            examineeAnswerDetail.setMarked(false);
            examineeAnswerDetail.setScore(BigDecimal.ZERO);
            examineeAnswerDetail.setSequenceIndex(1);
            examineeAnswerDetailList.add(examineeAnswerDetail);

            ExamingMonitorLog examingMonitorLog = new ExamingMonitorLog();
            examingMonitorLog.setExamineeArrangeID(String.valueOf(i));
            examingMonitorLog.setTypeID(i);
            examingMonitorLog.setLogTime("2019-03-21 15:14:38");
            examingMonitorLog.setMemo("1231231");
            examingMonitorLog.setReason("12334543");
            examingMonitorLogs.add(examingMonitorLog);
        }
        System.out.println(GsonUtil.toJson(examingMonitorLogs));
        System.out.println(GsonUtil.toJson(examineeAnswerDetailList));
    }

}
